package alo.p11.work;

/**
 * @ClassName Search
 * @Description 一个数组先增后降，查找最大元素
 * @Author Fanxc
 * @DATE 2021/3/12 21:18
 * @Version 1.0
 */
public class Search {
    int search(int[] A,int n){
        int mid,first=0,last=n-1;
        while (first<=last){
            if (first==last){
                return A[first];
            }else if (first==last-1){
                return Math.max(A[first],A[last]);
            }else {
                mid=first+(last-first)/2;
                if (A[mid-1]<A[mid]&&A[mid]>A[mid+1]){
                    return A[mid];
                }else if (A[mid-1]<A[mid]&&A[mid]<A[mid+1]){
                    first=mid+1;
                }else if (A[mid-1]>A[mid]&&A[mid]>A[mid+1]){
                    last=mid-1;
                }
            }
        }
        return 0;
    }
}
